﻿// ===============================================================================
// Project Name        :    Gfw.BLL.Basic.Facilities
// Project Description :    
// ===============================================================================
// Class Name          :    AccruedAward
// Namespace:          :    Gfw.BLL.Basic.Facilities
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/6/24 14:55:40
// Update Time         :    2012/6/24 14:55:40
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.BLL.Basic.Account;
using Gfw.BLL.Basic.ExceptionImpl;
using Gfw.Models.Account;
using Gfw.Repositories.Account;
using Gfw.Repository;

namespace Gfw.BLL.Basic.Facilities
{
    /// <summary>
    /// 预提动态奖金功能
    /// </summary>
    public class AccruedAward : IFacility
    {
        ActUserAccountinformation userAmount;
        double amount;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userAmount">用户的帐户信息，缴纳预提奖金费用以后，将从帐户信息扣除年费金额，但是并不回写到数据库</param>
        /// <param name="amount">动态奖金预提金额</param>
        public AccruedAward(ActUserAccountinformation userAmount, double amount)
       {
           if (userAmount.ElectronicMoney < amount)
           {
               throw new GfwException(ConstMessage.EBalanceLessThan);
           }
           this.userAmount = userAmount;
           this.amount = amount; 
       }

        /// <summary>
        /// 注意：作为联合事务，unitOfWork不为空的时候，不更新用户帐户信息表；否则更新帐户信息表
        /// </summary>
        /// <param name="unitOfWork"></param>
        public void Execute(Repository.UnitOfWork unitOfWork = null)
        {
            UnitOfWork work = unitOfWork ?? new UnitOfWork();
            userAmount.ElectronicMoney = userAmount.ElectronicMoney - amount;


            var userEMoneyRepository = new ActUserBargain4emoneyRepository();
            var companyBargainRepository = new ActCompanyBargainRepository();
            //添加用户电子笔交易记录
            userEMoneyRepository.AddOutlay(userAmount.Userid, (int)UserTradeTypeEnum.Accrued, amount, userAmount.ElectronicMoney,null,null, work);
            //添加公司
            companyBargainRepository.AddIncome(userAmount.Userid, (int)CompanyTradeTypeEnum.Accrued, amount, "", null, null, work);

            //作为单事务，需要提交更改
            if (unitOfWork == null)
            {
                var userAmountRepository = new ActUserAccountinformationRepository();
                userAmountRepository.Update(userAmount, work);
                work.Commit();
            }
        }
    }
}
